﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BCCommon;
using BCDto.MakeStaticPagingComponent.PageTemplate;
using BCService.MakeStaticPagingComponent.PageTemplateService;

namespace BCTMSRESTful.Controllers.Management.MakeStaticPagingComponent
{
    /// <summary>
    /// 页面模板控制器
    /// </summary>
    [ApiExplorerSettings(GroupName = "MakeStaticPage")]
    [Route("api/v1/sim/[controller]")]
    public class PageTemplateController : ManagementBaseController
    {
        private readonly IPageTemplateService pageTemplateService;

        public PageTemplateController(IPageTemplateService pageTemplateService)
        {
            this.pageTemplateService = pageTemplateService;
        }

        /// <summary>
        /// 获取页面模板信息
        /// </summary>
        /// <param name="id">页面模板信息Id</param>
        /// <response code="200">返回网站配置服务信息。</response>
        /// <returns></returns>
        [HttpGet("{id}")]
        [ProducesResponseType(typeof(PageTemplateDto), 200)]
        public IActionResult Get([FromRoute(Name ="id")]long id)
        {
            return this.Ok(this.pageTemplateService.Get(id));
        }

        /// <summary>
        /// 获取已选定网站模板的页面模板信息列表
        /// </summary>
        /// <response code="200">返回网站配置服务信息。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<PageTemplateDto>), 200)]
        public IActionResult GetList()
        {
            return this.Ok(this.pageTemplateService.GetAllPageTemplateList());
        }
       
        /// <summary>
        /// 获取页面模板信息列表(分页)
        /// </summary>
        /// <param name="pageName">页面名称</param>
        /// <param name="websiteTemplateId">网站模板Id</param>
        /// <param name="ascending">排序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">每页数据条数</param>
        /// <response code="200">返回页面模板信息。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<PageTemplateDto>), 200)]
        public IActionResult Get([FromQuery(Name = "page_name")]string pageName,
                [FromQuery(Name ="website_template_id")]int websiteTemplateId,
                [FromQuery(Name = "ascending")]bool? ascending = true,
                [FromQuery(Name = "index")]int index = 1,
                [FromQuery(Name = "size")]int size = 20)
        {
            return this.Ok(this.pageTemplateService.Get(pageName, websiteTemplateId, ascending, index,size));
        }
        /// <summary>
        /// 获取指定页面模板子级列表
        /// </summary>
        /// <response code="200">返回页面模板信息。</response>
        /// <returns></returns>
        [HttpGet("[action]/{website_template_id}")]
        [ProducesResponseType(typeof(IEnumerable<PageTemplateDto>), 200)]
        public IActionResult Tree([FromRoute(Name = "website_template_id")]int websiteTemplateId)
        {
            return this.Ok(this.pageTemplateService.GetAnyWebsiteTemplateTree(websiteTemplateId));
        }

        /// <summary>
        /// 获取指定页面模板子级列表
        /// </summary>
        /// <response code="200">返回页面模板信息。</response>
        /// <returns></returns>
        [HttpGet("[action]/{website_template_id}")]
        [ProducesResponseType(typeof(IEnumerable<PageTemplateDto>), 200)]
        public IActionResult Children([FromRoute(Name = "website_template_id")]int websiteTemplateId,
            [FromQuery(Name ="parent_page_code")]string parentPageCode)
        {
            return this.Ok(this.pageTemplateService.GetPageTemplateChildren(websiteTemplateId, parentPageCode));
        }
      
        /// <summary>
        /// 添加页面模板信息
        /// </summary>
        /// <param name="pageTemplateRequestDto">页面模板信息</param>
        /// <response code="201">返回页面模板信息。</response>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(PageTemplateDto), 201)]
        public IActionResult Post([FromBody]PageTemplateRequestDto pageTemplateRequestDto)
        {
            PageTemplateDto result = this.pageTemplateService.Add(pageTemplateRequestDto);
            return this.Created(string.Format("api/v1/sim/PageTemplate/{0}", result.PageTemplateId), result);
        }

        /// <summary>
        /// 修改页面模板信息
        /// </summary>
        /// <param name="id">页面模板信息Id</param>
        /// <param name="pageTemplateRequestDto">页面模板信息</param>
        /// <response code="200">返回页面模板信息。</response>
        /// <returns></returns>
        [HttpPut("{id}")]
        [ProducesResponseType(typeof(PageTemplateDto), 200)]
        public IActionResult Put([FromRoute(Name ="id")]long id, [FromBody]PageTemplateRequestDto pageTemplateRequestDto)
        {
            return this.Ok(this.pageTemplateService.Update(id, pageTemplateRequestDto));
        }

        /// <summary>
        /// 删除页面模板信息
        /// </summary>
        /// <param name="id">页面模板信息Id</param>
        /// <response code="204">无内容。</response>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IActionResult Delete([FromRoute(Name = "id")]long id)
        {
            this.pageTemplateService.Delete(id);
            return this.NoContent();
        }

        /// <summary>
        /// 获取所有节点信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<PageTemplateDto>), 200)]
        public IActionResult GetChild()
        {
            return this.Ok(this.pageTemplateService.GetAllChild());
        }
    }
}
